<?php

class StudyController extends Controller
{
    /*
    * 添加学装修信息
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $title = Util::getParamNotEmpty("title");
            $pic_url = Util::getParamNotEmpty("pic_url");
            $link_url = Util::getParamNotEmpty("link_url");




            //添加
            $obj = new StudyInfolist();
            Util::initObjData($obj);


            $obj->title = $title;
            $obj->pic_url = $pic_url;
            $obj->link_url = $link_url;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $title = Util::getParamNotEmpty("title");
            $pic_url = Util::getParamNotEmpty("pic_url");
            $link_url = Util::getParamNotEmpty("link_url");


            //验证存在
            $obj = StudyInfolist::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);

            //修改信息

            $obj->title = $title;
            $obj->pic_url = $pic_url;
            $obj->link_url = $link_url;

            //保存
            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = StudyInfolist ::model()->findByPK($obj_id);

                    Util::checkExistAndNotDelete($obj);

                    Util::deleteObjData($obj);

                    Util::saveObjData($obj);



                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();


            //获取count
            $totalcount = StudyInfolist ::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time  desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list = StudyInfolist::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
   * 获取评论列表分页
   *
   */
    public function actioncommentlist()
    {

        try {
            //参数验证

            $study_id = Util::getParamNotEmpty("study_id");

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStrAdd($searchsql,"study_id",$study_id);


            //获取count
            $totalcount = StudyComment ::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by create_time  desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = StudyComment ::model()->findAll($searchsql);

            $detailarray = array();

            for($i=0;$i<count($list);$i++)
            {
                $obj = $list[$i];

                $user = UcUser::model()->findByPK($obj->user_id);

                $detailarray["user"] = UserService::formatUser($user) ;

                if($obj->parentcomment_id >0)
                {
                    $pcom = StudyComment::model()->findByPK($obj->id);

                    if(empty($pcom)==false) {

                        $puser = UcUser::model()->findByPK($pcom->user_id);

                        $detailarray["puser"] = UserService::formatUser($puser) ;



                    }

                }

                $obj->text_str = $detailarray;




            }


            Util::responseList($totalcount, $list);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
   * 获取点赞列表分页
   *
   */
    public function actionstarlist()
    {

        try {
            //参数验证

            $study_id = Util::getParamNotEmpty("study_id");

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStrAdd($searchsql,"study_id",$study_id);


            //获取count
            $totalcount = StudyComment ::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by create_time  desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = StudyStar ::model()->findAll($searchsql);

            $detailarray = array();

            for($i=0;$i<count($list);$i++)
            {
                $obj = $list[$i];

                $user = UcUser::model()->findByPK($obj->user_id);

                $detailarray["user"] = UserService::formatUser($user) ;



                $obj->text_str = $detailarray;




            }


            Util::responseList($totalcount, $list);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }

    /*
    * 删除评论
    */
    public function actionDeletecomment()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //事务开始
            $connection = Yii::app()->db;

            $transaction = $connection->beginTransaction();

            try {

                //删除
                $obj = StudyComment::model()->findByPK($obj_id);

                Util::checkExistAndNotDelete($obj);

                Util::deleteObjData($obj);

                Util::saveObjData($obj);

                //评论数-1

                $study = StudyInfolist::model()->findByPK($obj->study_id);

                Util::checkExistAndNotDelete($study);

                $study->comment_count -=1;

                Util::updateObjData($study);
                Util::saveObjData($study);




                $transaction->commit();//事务结束
            } catch (Exception $e) {
                $transaction->rollback();//回滚函数

                throw new Exception($e->getMessage());

            }




                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

}